package lancs.mobilemedia.core.ui.controller.commands;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;

import lancs.mobilemedia.core.ui.controller.AbstractController;
import lancs.mobilemedia.core.ui.controller.ControllerCommand;
import lancs.mobilemedia.core.ui.controller.PhotoController;
import lancs.mobilemedia.core.ui.controller.PhotoListController;
import lancs.mobilemedia.core.ui.controller.ScreenSingleton;
import lancs.mobilemedia.core.util.Constants;
import lancs.mobilemedia.lib.exceptions.ImageNotFoundException;
import lancs.mobilemedia.lib.exceptions.PersistenceMechanismException;

public class DeletePhoto implements ControllerCommand {

	public boolean execute(AbstractController controller) {
		String selectedImageName = ((PhotoController) controller).getSelectedImageName();
		try {
			controller.getAlbumData().deleteImage(controller.getCurrentStoreName(), selectedImageName);
		} catch (PersistenceMechanismException e) {
			Alert alert = new Alert("Error", "The mobile database can not delete this photo", null, AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert,
					Display.getDisplay(controller.getMidlet()).getCurrent());
			return true;
		} catch (ImageNotFoundException e) {
			Alert alert = new Alert("Error", "The selected photo was not found in the mobile device", null,
					AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert,
					Display.getDisplay(controller.getMidlet()).getCurrent());
			return true;
		}
		((PhotoListController) controller).showImageList(controller.getCurrentStoreName(), false, false);
		ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
		return true;
	}
}
